
package ar.camera.globject;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

public class Plane
{
	private FloatBuffer mFHorizontalVertexBuffer;
	private FloatBuffer mFVerticalVertexBuffer;
    private ByteBuffer  mHorizontalColorBuffer;
    private ByteBuffer  mVerticalColorBuffer;
    private ByteBuffer  mHorizontalIndexBuffer;
    private ByteBuffer  mVerticalIndexBuffer;
    byte horizontalIndices[];
    byte verticalIndices[];
    byte horizontalColors[];
    byte verticalColors[];
    float horizontalVertices[];
    float verticalVertices[];
    
    float rightX = 200;
    float leftX = -200;
    float topZ = -200;
    float bottomZ = 200;
    float planeY = -15;
    
    public Plane()
    {
        horizontalVertices = new float[0];
        for(int i = (int) topZ; i <= bottomZ; i += 10){
        	horizontalVertices = addFloatElementToArray(horizontalVertices, leftX);
        	horizontalVertices = addFloatElementToArray(horizontalVertices, planeY);
        	horizontalVertices = addFloatElementToArray(horizontalVertices, i);
        	
        	horizontalVertices = addFloatElementToArray(horizontalVertices, rightX);
        	horizontalVertices = addFloatElementToArray(horizontalVertices, planeY);
        	horizontalVertices = addFloatElementToArray(horizontalVertices, i);
        }
        
        verticalVertices = new float[0];
        for(int i = (int) leftX; i <= rightX; i += 10){
        	verticalVertices = addFloatElementToArray(verticalVertices, i);
        	verticalVertices = addFloatElementToArray(verticalVertices, planeY);
        	verticalVertices = addFloatElementToArray(verticalVertices, bottomZ);
        	
        	verticalVertices = addFloatElementToArray(verticalVertices, i);
        	verticalVertices = addFloatElementToArray(verticalVertices, planeY);
        	verticalVertices = addFloatElementToArray(verticalVertices, topZ);
        }
        
        horizontalIndices = new byte[0];
        for(int i = 0; i < horizontalVertices.length / 3; i++){
        	horizontalIndices = addByteElementToArray(horizontalIndices, (byte)i);
        }
        verticalIndices = new byte[0];
        for(int i = 0; i < verticalVertices.length / 3; i++){
        	verticalIndices = addByteElementToArray(verticalIndices, (byte)i);
        }
        
        byte maxColor=(byte)255;
        horizontalColors = new byte[0]; 
        for(int i = 0; i < horizontalVertices.length / 3; i++){
        	horizontalColors = addByteElementToArray(horizontalColors, maxColor);
        	horizontalColors = addByteElementToArray(horizontalColors, maxColor);
        	horizontalColors = addByteElementToArray(horizontalColors, maxColor);
        	horizontalColors = addByteElementToArray(horizontalColors, maxColor);
        }        
        verticalColors = new byte[0]; 
        for(int i = 0; i < verticalVertices.length / 3; i++){
        	verticalColors = addByteElementToArray(verticalColors, maxColor);
        	verticalColors = addByteElementToArray(verticalColors, maxColor);
        	verticalColors = addByteElementToArray(verticalColors, maxColor);
        	verticalColors = addByteElementToArray(verticalColors, maxColor);
        }               

        ByteBuffer hvbb = ByteBuffer.allocateDirect(horizontalVertices.length * 4);
        hvbb.order(ByteOrder.nativeOrder());
        mFHorizontalVertexBuffer = hvbb.asFloatBuffer();
        mFHorizontalVertexBuffer.put(horizontalVertices);
        mFHorizontalVertexBuffer.position(0);
        
        ByteBuffer vvbb = ByteBuffer.allocateDirect(verticalVertices.length * 4);
        vvbb.order(ByteOrder.nativeOrder());
        mFVerticalVertexBuffer = vvbb.asFloatBuffer();
        mFVerticalVertexBuffer.put(verticalVertices);
        mFVerticalVertexBuffer.position(0);
        
        mHorizontalColorBuffer = ByteBuffer.allocateDirect(horizontalColors.length);
        mHorizontalColorBuffer.put(horizontalColors);
        mHorizontalColorBuffer.position(0);
        
        mVerticalColorBuffer = ByteBuffer.allocateDirect(verticalColors.length);
        mVerticalColorBuffer.put(verticalColors);
        mVerticalColorBuffer.position(0);

        mHorizontalIndexBuffer = ByteBuffer.allocateDirect(horizontalIndices.length);
        mHorizontalIndexBuffer.put(horizontalIndices);
        mHorizontalIndexBuffer.position(0);
        
        mVerticalIndexBuffer = ByteBuffer.allocateDirect(verticalIndices.length);
        mVerticalIndexBuffer.put(verticalIndices);
        mVerticalIndexBuffer.position(0);
    }

    public void draw(GL10 gl)
    {
        gl.glFrontFace(GL11.GL_CW);
        gl.glLineWidth(3f);
        
        gl.glVertexPointer(3, GL11.GL_FLOAT, 0, mFHorizontalVertexBuffer);
        gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mHorizontalColorBuffer);
        gl.glDrawElements(GL11.GL_LINES, horizontalIndices.length, GL11.GL_UNSIGNED_BYTE, mHorizontalIndexBuffer);
        
        gl.glVertexPointer(3, GL11.GL_FLOAT, 0, mFVerticalVertexBuffer);
        gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mVerticalColorBuffer);
        gl.glDrawElements(GL11.GL_LINES, verticalIndices.length, GL11.GL_UNSIGNED_BYTE, mVerticalIndexBuffer);
        
        gl.glFrontFace(GL11.GL_CCW);
    }    
    
    public float[] addFloatElementToArray(float[] a, float k){
    	float[] tmp = new float[a.length + 1];
    	for(int i = 0; i < a.length; i++){
    		tmp[i] = a[i];
    	}
        tmp[a.length] = k;

        return tmp;
    }
    
    public byte[] addByteElementToArray(byte[] a, byte k){
    	byte[] tmp = new byte[a.length + 1];
    	for(int i = 0; i < a.length; i++){
    		tmp[i] = a[i];
    	}
        tmp[a.length] = k;

        return tmp;
    }
}
